/* Copyright(c) 1998-2012, Arnaud Colin
 * All rights reserved.
 *
 * Licence GNU GPL version 3
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   -> Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   -> Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 */


#include "stdafx.h"
#include "Version.h"
#include <wchar.h> 
#include <string.h> 




#if _WIN32_WCE
wchar_t *wcstok_s( wchar_t* strToken, const wchar_t* strDelimit, wchar_t** strNextToken )
{
	return wcstok( strToken, strDelimit );
}
#endif


CVersion::CVersion(void)
{
	major = 0;
    minor= 0;
    build= 0;
    revision= 0;
}

CVersion::~CVersion(void)
{
}

    CVersion::CVersion(TCHAR Ver[] )
	{

		unsigned long int num=0,val;
		TCHAR *tok,*ptr, *ntok = NULL;
		tok=wcstok_s(Ver,L".", &ntok);
		int i = 0;
		while( tok != NULL)
		{		
			i++;
			val=wcstoul(tok,&ptr,0);
			switch(i)
			{
				case 1:
					{
						major = val;
						break;
					}
				case 2:
					{
						minor= val;
						break;
					}
				case 3:
					{
						build= val;
						break;
					}
				case 4:
					{
						revision= val;
						break;
					}

			}
			tok=wcstok_s(NULL,L".", &ntok);
		}		
	}
  
unsigned long int conv(wchar_t ipadr[])
{
    unsigned long int num=0,val;
    wchar_t *tok,*ptr, *ntok = NULL;
	tok=wcstok_s(ipadr,L".", &ntok);	
    while( tok != NULL)
    {
		val=wcstoul(tok,&ptr,0);		
        num=(num << 8) + val;
        tok=wcstok_s(NULL,L".", &ntok);
    }
    return(num);
}


 
CVersion::CVersion(int major, int minor, int build =0, int revision =0)
{
	CVersion::major = major;
    CVersion::minor= minor;
    CVersion::build= build;
    CVersion::revision= revision;
}



int CVersion::CompareTo(const CVersion &source)
 {
	     
		if (CVersion::major != source.major)
        {
            if (CVersion::major > source.major)
            {
                return 1;
            }
            return -1;
        }
        if (CVersion::minor != source.minor)
        {
            if (CVersion::minor > source.minor)
            {
                return 1;
            }
            return -1;
        }
        if (CVersion::build != source.build)
        {
            if (CVersion::build > source.build)
            {
                return 1;
            }
            return -1;
        }
        if (CVersion::revision == source.revision)
        {
            return 0;
        }
        if (CVersion::revision > source.revision)
        {
            return 1;
        }
        return -1;
	 return 0;
}

	 bool CVersion::operator<(const CVersion &source)
	 {
		 return (this->CompareTo(source) < 0);
	 }
    bool CVersion::operator>(const CVersion &source)
	 {
		 return (this->CompareTo(source) > 0);
	 }

    bool CVersion::operator<=(const CVersion &source)
	 {
		  return (this->CompareTo(source) <= 0);
	 }

    bool CVersion::operator>=(const CVersion &source)
	{
		 return (this->CompareTo(source) >= 0);
	 }

	bool CVersion::operator==(const CVersion &source)
	{
		return (this->CompareTo(source) == 0);
	}

	bool CVersion::operator>=(TCHAR Ver[])
	{
		CVersion* tmp = new CVersion(Ver);
		bool bt = (this->CompareTo(*tmp) >= 0);
		delete tmp;
		return bt;
	}
